(function($) {
    $.addPicker = function(t) {
        var colorSelector = {
            init: function(){
                $(t).ColorPicker({
                    onShow: function (colpkr) {
                        $(colpkr).fadeIn(500);
                        colorSelector.current = this;
                        var value = $(this).parent().find('input').attr('value');
                        $(this).ColorPickerSetColor(value);
                        return false;
                    },
                    onHide: function (colpkr) {
                        $(colpkr).fadeOut(500);
                        return false;
                    },
                    onChange: function (hsb, hex, rgb) {
                        var el = colorSelector.current;
                        $(el).find('div').css('backgroundColor', '#' + hex);
                        var name_encoded =  $(el).parent().find('input').attr('name');
                        //Decode name
                        var name_decoded = name_encoded.split("/");
                        var selector = name_decoded[0].replace(/_/g, " ");
                        selector = selector.replace(/\|/g, ".")
                        var property = name_decoded[1];

                        $(selector).css(property, '#' + hex);
                        $(el).parent().find('input').attr('value', '#'+hex);
                    }
                });
            }
        };
        colorSelector.init();
    };

    $.setDefaultCustomization = function(){
        var data = {
            action : 'set_default_customization'
        };
        var send = $.param(data);
        var message = "";
        $.ajax({
            url : 'ajax/message-ajax.php',
            data : send,
            dataType : 'html',
            type : 'POST',
            success : function(data) {
                message = data;
                var c = confirm(message);
                if (c) {
                    $('.ajax-load').show();

                    $.ajax({
                        url : 'ajax/theme-ajax.php',
                        data : send,
                        dataType : 'json',
                        type : 'POST',
                        success : function(data){
                            $(".ajax-load").hide();
                            if (data.code == 1) {
                                alert(data.message);
                                location.reload(true);
                            }
                            else {
                                $(".ajax-message").htmlTimer(data.message, 5000);
                            }
                        },
                        error : function() {
                            $('.ajax-load').hide();
                            $(".ajax-message").htmlTimer("Error", 5000);
                        }
                    });
                }
            },
            error : function(){
                $(".ajax-message").htmlTimer("Error", 5000);
            }
        });
        
    };
})(jQuery);

$(function(){
    $.addPicker('.color-selector');

    $('.cancel').click(function(){
        location.reload(true);
        return false;
    });

    $('.default').click(function(){
        $.setDefaultCustomization();
        return false;
    });
    

});